﻿


import Request from '../../base/request.js?t=202511041006'
import Storage from '../../base/storage.js?t=202511041006'
import { imageBaseUrl, isImageFile, dealTime, getRecentYears, downloadfun, getBrowserInfo } from "../../base/utils.js?t=202511041006"
const Composition = (proxy) => {
    const data = reactive({
        messageTest: "",
        proxy: "",
        popType: 0,
        popShow:false,
        pagination: {
            total: 0,
            current: 1,
            loading: false,
            pageSize: 10,
            showTotal: total => `总共 ${total} 项`,
            defaultPageSize: 10,
            showSizeChanger: true, // 是否显示pagesize选择
            showQuickJumper: false, // 是否显示跳转窗

        },
        uploadLock:false,
        previewShow: false,
        imgUrl: "",
        cdeptList: [],
        sdeptList:[],

        cid:"",
        deptList: null,
        hdeptList:[],
        deptTitle: null,
        deptListColumns:[{
            title: '序号',
            dataIndex: 'no',
            key: 'no',
            align: 'center',
            width: 80,
            customRender: ({ index }) => {
                return (params.pagination.value.current - 1) * params.pagination.value.pageSize + index + 1
            },
        },
        {
            title: '部门',
            dataIndex: 'DepartmentName',
            key: 'DepartmentName',
            align: 'center'
        },
        {
            title: '通知人',
            dataIndex: 'UserList',
            key: 'UserList',
            align: 'center',
            customRender: ({ text }) => {
           


                let divs = text.map((e, i) => {
                    return h('div', {
                        style: `width: 80%;
                            display: flex;
                            margin:0 auto;
                          `

                    }, {
                        default: () => `${e.UserInfo.UserName}(${e.UserInfo.UserNo})`
                    })
                })

                return h('div', {
                    style: `width: 100%;`
                },
                    [...divs]
                );
            },

        }],
        noticePreView: {
            title: "",
            fileList: [],
            noticePerson: [

            ],
            cctarget: [

            ],

            EmailBody: "",
            columns: [{
                title: '序号',
                dataIndex: 'no',
                key: 'no',
                align: 'center',
                width: 80,
                customRender: ({ index }) => {
                    return index+1
                },
            },
            {
                title: '姓名',
                dataIndex: 'UserName',
                key: 'UserName',
                align: 'center',
                customRender: ({ record }) => {
                    return `${record.UserName}`
                },

            },

                {
                    title: '部门',
                    dataIndex: 'DepartmentName',
                    key: 'DepartmentName',
                    align: 'center',

                },
            {
                title: '邮箱',
                dataIndex: 'Email',
                key: 'Email',
                align: 'center',

            }],
   
        },
        rowSelection: {
            onChange: (selectedRowKeys, selectedRows) => {
                params.rowSelection.value.selectedRowKeys = params.cdeptList.value
          
            },

            onSelectAll: (a, b) => {
                params.cdeptList.value = b.length == 0 ? [] : b.map(e => e.DepartmentId)
            },
            onSelect: (a, b, c) => {
     
                if (b) {
                    params.rowSelection.value.selectedRowKeys.includes(a.DepartmentId) ? console.log('aaa') : params.rowSelection.value.selectedRowKeys.push(a.DepartmentId)
                } else {
                    let index = params.rowSelection.value.selectedRowKeys.findIndex(e => e == a.DepartmentId)
                    if (index != -1) {

                        params.rowSelection.value.selectedRowKeys.includes(a.DepartmentId) ? params.rowSelection.value.selectedRowKeys.splice(index, 1) : console.log('bbb')

                    }
             
                }
            },
            selectedRowKeys:[],

            getCheckboxProps: record => ({
                disabled: record.Status == 0 || record.Status == 2,

            })
        },
        columns: [
            {
                title: '序号',
                dataIndex: 'a',
                key: 'a',
                align: 'center',
                width: 80,
                customRender: ({ index }) => {
                    return (params.pagination.value.current - 1) * params.pagination.value.pageSize + index + 1
                },
            },
            {
                title: '通知标题',
                dataIndex: 'Title',
                key: 'Title',
                align: 'center'

            },
            {
                title: '通知部门',
                dataIndex: 'Departments',
                key: 'Departments',
                align: 'center',
                customRender: function ({ text, record }) {


                    let content = h('div', {
                        style: 'width:700px'
                    }, { default: () => record.Departments.map(e => e.DepartmentName).join(` , `) });

                       
                    let textc = h(antd.Button, {
                        type: "link"
                    }, { default: () => record.Departments.length });

                    return h(antd.Popover, {
                        title: '通知部门',
                        content: content,

                    }, () => [textc]);
                }
            },
            {
                title: '发布日期',
                dataIndex: 'UpdateTime',
                key: 'UpdateTime',
                align: 'center',
                customRender: function ({ text, record }) {
                    return dealTime(text)
                }
            },
            {
                title: '发布人',
                dataIndex: 'CreateUserName',
                key: 'CreateUserName',
                align: 'center',
                customRender: function ({ text, record }) {
                    return `${record.CreateUserName}(${record.CreateUser})`
                }
            },
            {
                title: '状态',
                dataIndex: 'Status',
                key: 'Status',
                align: 'center',
                width: '100px',
                customRender: function ({ text }) {
                 
                   return  h(antd.Tag, {
                       color: text == '1' ? "#108ee9" : "#87d068",
                        style: 'cursor:pointer'
                    }, { default: () => text==1?'发布':'保存' });

               


                }
            },




            {
                title: '操作',
                key: 'action',
                align: 'center',
                fixed: 'right',
                width: 100,
                customRender: function ({ text, record, index, column }) {

                    const returnBtn = h(antd.Button, {
                        style: 'margin-right:4px',
                        type: "link",
                        size: 'small',
                    }, { default: () => '撤回' })

                    const fReturnBtn = h(antd.Popconfirm, {
                        title: "确定撤回此数据？",
                        onConfirm: () => methods.handleReturn(record.Id)

                    }, { default: () => returnBtn })



                    const delBtn = h(antd.Button, {
                        style: 'margin-right:4px',
                        type: "link",
                        size: 'small',
                        danger:true
                    }, { default: () => '删除' })

                    const fDelBtn = h(antd.Popconfirm, {
                        style:'width:220px!important;',
                        title: `提示：通知标题【${record.Title}】的考核计划，删除后将无法恢复，请您确认。`,
                        onConfirm: () => methods.handleDelete(record.Id)
                    }, { default: () => delBtn })


                    const modifyBtn = h(antd.Button, {
                        style: 'margin-right:4px',
                        size:'small',
                        type: "link",
                        onClick: () => methods.handleModify(record)
                    }, { default: () => '编辑' })



                    const publicBtn = h(antd.Button, {
                        style: 'margin-right:4px',
                        type: "link",
                        size: 'small',
                        onClick: () => methods.handlePublic(record)
                    }, { default: () => '通知' })



                    const detailBtn = h(antd.Button, {
                        style: 'margin-right:4px',
                        type: "link",
                        size: 'small',
                        onClick: () => methods.handleDetail(record)
                    }, { default: () => '详情' })

                    return h('div', {
                        style: 'width:200px'
                    },
                        {
                            default:() => [record.IsShowWithdraw ? fReturnBtn : null, record.IsShowEdit ? modifyBtn : null, record.IsShowEdit ? fDelBtn : null, publicBtn, detailBtn]
                        }
                    );

                }
            },
        ],
        dataSource: [],


        yearsList: [],
        queryParams: {
            "Title": null,
            "Years": null,
            "Status": null
        },
        statusList: [
            { label: "保存", value: 0 },
            { label: "发布", value: 1 }
        ]

      
    })
    let params = toRefs(data)


    const methods = {
        sdownloadfun: (name, url) => {
            downloadfun(name,url)
        },
        sChange: (e) => {

            params.sdeptList.value = params.deptList.value.filter(es => {

                return es.DepartmentName.indexOf(params.deptTitle.value) != -1
            })
            if (!params.deptTitle.value) {
                params.sdeptList.value = params.deptList.value
            }
        },

        PreviewEmailContent: (id,ids) => {
   
            Request({
                url: "/performance/PreviewEmailContent",
                method: 'post',
                data: {
                    PlanId: params.cid.value,
                    DepartmentList: params.cdeptList.value
                },
            }).then(res => {

                params.noticePreView.value.EmailBody = res.Data.EmailBody

                params.noticePreView.value.noticePerson = res.Data.To

                params.noticePreView.value.cctarget = res.Data.Cc

                params.noticePreView.value.fileList = res.Data.Attachment

                params.noticePreView.value.title = res.Data.Subject
    
            })
        },

        SendPlanNoticeEmail: () => {
            params.uploadLock.value=true
            return new Promise((reslove, rej) => {
                Request({
                    url: "/performance/SendPlanNoticeEmail",
                    method: 'post',
                    data: {
                        PlanId: params.cid.value,
                        DepartmentList: params.cdeptList.value
                    },
                }).then(res => {

                    params.messageTest.value.info(res.Msg);
                    methods.getTableList(true)
                    params.uploadLock.value = false
                    reslove()
                })

            })

        },


        GetPlanDepartmentList: (id) => {
            return new Promise((reslove, rej) => {

                Request({
                    url: "/performance/GetPlanDepartmentList",
                    method: 'post',
                    params: {
                        PlanId: id
                    },
                }).then(res => {

                   

                    reslove(1)



                    params.deptList.value = res.Data

                    params.sdeptList.value = res.Data

                    params.cdeptList.value = params.deptList.value.map(e => e.DepartmentId)
                    
                    params.hdeptList.value = params.deptList.value.map(e => {
                        return {
                            label: e.DepartmentName,
                            value: e.DepartmentId
                        }
                    })

                    params.rowSelection.value.selectedRowKeys = params.cdeptList.value
                    //params.cdeptList.value = selectedRows.map(e => +e.DepartmentId)

                    //params.messageTest.value.info(res.Msg);
                    //methods.getTableList(true)
                })
            })
        },


  
                handlePublic:(record) => {
                    params.popType.value = 0

                    params.cid.value = record.Id

                    methods.GetPlanDepartmentList(record.Id).then(res => {
                        params.popShow.value = true
                    })


                },






        setVisible: (value) => {
            params.previewShow.value = value;
        },
        handleReturn: (id) => {
            Request({
                url: "/performance/WithdrawSimplePlan",
                method: 'post',
                data: {
                   Id:id
                },
            }).then(res => {
                params.messageTest.value.info(res.Msg);
                methods.getTableList(true)
            })
        },
        handleDelete: (id) => {
            Request({
                url: "/performance/DeleteSimplePlan",
                method: 'post',
                data: {
                    Id: id
                },
            }).then(res => {
                params.messageTest.value.info(res.Msg);
                methods.getTableList(true)
            })
        },
        handleDetail: (record) => {
            window.location.href = `/performance/PerformancePlanAddNew?id=${record.Id}&optype=2`
        },
        handlePopOk: () => {
            if (params.popType.value == 0) {
                params.popType.value = 1


                methods.PreviewEmailContent(params.cid.value, params.cdeptList.value)



            } else {


                methods.SendPlanNoticeEmail().then(res => {

                    params.popType.value = 0
                    params.popShow.value = false

                })
               
      
            }
        },


        handleTableChange: (page) => {
            const { current, pageSize } = page
            params.pagination.value.current = current;
            params.pagination.value.pageSize = pageSize;
            methods.getTableList()
        },
        addPlan: () => {
            window.location.href ='/performance/PerformancePlanAddNew'
        },

        handleModify: (record) => {
            window.location.href = `/performance/PerformancePlanAddNew?id=${record.Id}`
        },
        handleQuery: () => {
            methods.getTableList(true)
        },
        getTableList: (isRresh) => {
            if (isRresh) {
                params.pagination.value.current = 1
            }
            params.pagination.value.loading = true
            Request({
                url: "/performance/GetSimplePlanPageList",
                method: 'post',
                params: {
                    page: params.pagination.value.current,
                    pageSize: params.pagination.value.pageSize,
                    ...params.queryParams.value
                },
            }).then(res => {
                params.dataSource.value = res.Data.Data
                params.pagination.value.total = res.Data.TotalItemCount
                params.pagination.value.loading = false
            })
        },


 





        init: () => {
              getBrowserInfo().catch(err => { console.log(err)})
            if (proxy) {
                params.proxy.value = proxy
            }
            const { message } = antd
            params.messageTest.value = message
            params.messageTest.value.config({
                top: `10vh`,
                duration: 7,
                maxCount: 1,
                rtl: true,
            });





            params.yearsList.value = getRecentYears()
            let info = Storage.get('AccountInfo', true)
            Request({
                url: "/Account/createJwtToken",
                method: 'post',
                data: {
                    "userName": info && info.UserNo
                }
            }).then(res => {

                Storage.set('token', res.Data,true)
                methods.getTableList(true)
                //methods.getDeptList()



            })
        }
    }
    //<a-button v-on:click="onSave" type="primary">保存</a-button>
    //<a-button v-on:click="onSbumit" type="primary">提交</a-button>
    const template = {
        bottomBtns: {
            template: `<div class="w-full bg-[#eeeeee] h-50  flex  items-center justify-end">
                    <a-space>
                        <a-button v-on:click="onBack">返回</a-button>
                    </a-space>
                </div>`,
            setup() {

                return {
                    ...params,
                    ...methods,
                };
            }

        }
    }
    return {
        ...params,
        ...methods,
        template
    }
}

export default Composition
